package estyle.auto.nkornilova.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.internal.selenesedriver.GetElementValue;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

/**
 * Created with IntelliJ IDEA.
 * User: nkornilova
 * Date: 30.04.13
 * Time: 17:45
 * To change this template use File | Settings | File Templates.
 */
public class MessagePage extends AnyPage{

    public MessagePage(PageManager pages) {
        super(pages);
    }


    ////div[@class='tabs-header']//ul[@class='tabs']//span[text()='Сведения о плательщике']
    ////div[@class='tabs-header']//ul[@class='tabs']//span[text()='Сведения о банке (филиале)']
    ////div[@class='tabs-header']//ul[@class='tabs']//span[text()='Сведения о счете']

    @FindBy(xpath = "//div[@class='tabs-header']//ul[@class='tabs']//span[text()='Сведения о плательщике']")
    private WebElement insurerTab;

    @FindBy(xpath = "//div[@class='tabs-header']//ul[@class='tabs']//span[text()='Сведения о банке (филиале)']")
    private WebElement bankTab;

    @FindBy(xpath = "//div[@class='tabs-header']//ul[@class='tabs']//span[text()='Сведения о счете']")
    private WebElement accountTab;

    @FindBy(id = "appDialogForm")
    private WebElement messagePage;

    @FindBy(id = "bankEMessageEnterDate")
    private WebElement bankEMessageEnterDateField;

    @FindBy(id = "bankPMessageEnterDate")
    private WebElement bankPMessageEnterDateField;

    @FindBy(id = "payerPMessageEnterDate")
    private WebElement payerPMessageEnterDateField;

    @FindBy(id = "bankEMessage")
    private WebElement bankEMessageCheckBox;

    @FindBy(id = "bankPMessage")
    private WebElement bankPMessageCheckBox;

    @FindBy(id = "payerPMessage")
    private WebElement payerPMessageCheckBox;

    @FindBy(id = "fileName")
    private WebElement fileNameField;

    @FindBy(id = "typeDisp")
    private WebElement typeDispField;

    @FindBy(id = "kind")
    private WebElement kindComboBox;

    @FindBy(id = "status")
    private WebElement statusField;

    @FindBy(id = "docId")
    private WebElement docIdField;

    @FindBy(id = "identifier")
    private WebElement identifierField;

    @FindBy(id = "msgDate")
    private WebElement msgDateField;

    @FindBy(id = "person_ju")
    private WebElement person_juRadioButton;

    @FindBy(id = "person_ph")
    private WebElement person_phRadioButton;

    @FindBy(id = "insurerName")
    private WebElement insurerNameField;

    @FindBy(id = "stateRegCertSer")
    private WebElement stateRegCertSerField;

    @FindBy(id = "stateRegCertNum")
    private WebElement stateRegCertNumField;

    @FindBy(id = "clientOgrn")
    private WebElement clientOgrnField;

    @FindBy(id = "taxRegCertSer")
    private WebElement taxRegCertSerField;

    @FindBy(id = "taxRegCertNum")
    private WebElement taxRegCertNumField;

    @FindBy(id = "clientInn")
    private WebElement clientInnField;

    @FindBy(id = "clientKpp")
    private WebElement clientKppField;

    @FindBy(id = "bankShortName")
    private WebElement bankShortNameField;

    @FindBy(id = "taxAuthBank")
    private WebElement taxAuthBankField;

    @FindBy(id = "bankFullName")
    private WebElement bankFullNameField;

    @FindBy(id = "bankAddress")
    private WebElement bankAddressField;

    @FindBy(id = "bankRegNum")
    private WebElement bankRegNumField;

    @FindBy(id = "branchOrderNumber")
    private WebElement branchOrderNumberField;

    @FindBy(id = "bankBic")
    private WebElement bankBicField;

    @FindBy(id = "bankKpp")
    private WebElement bankKppField;

    @FindBy(id = "bankInn")
    private WebElement bankInnField;

    @FindBy(id = "bankOgrn")
    private WebElement bankOgrnField;

    @FindBy(id = "bankRepPosition")
    private WebElement bankRepPositionField;

    @FindBy(id = "bankRepFullName")
    private WebElement bankRepFullNameField;

    @FindBy(id = "bankPhone")
    private WebElement bankPhoneField;

    @FindBy(id = "accNumber")
    private WebElement accNumberField;

    @FindBy(id = "contractStatus")
    private WebElement contractStatusComboBox;

    @FindBy(id = "contractCreation")
    private WebElement contractCreationField;

    @FindBy(id = "contractNumber")
    private WebElement contractNumberField;

    @FindBy(id = "accStatus")
    private WebElement accStatusComboBox;

    @FindBy(id = "accCurr")
    private WebElement accCurrComboBox;

    @FindBy(id = "accType")
    private WebElement accTypeField;

    @FindBy(id = "accOpenDate")
    private WebElement accOpenDateField;

    @FindBy(xpath = "//span[text()=\"Сохранить\"]/..")
    private WebElement saveButton;

    @FindBy(xpath = "//span[text()=\"Добавить в реестр БС\"]/..")
    private WebElement addBankAccountButton;

    @FindBy(xpath = "//span[text()=\"Закрыть\"]/..")
    private WebElement closeButton;

    @FindBy(xpath = "//button[@onclick='selectInsurerWindowOpen()']")
    private WebElement selectInsurerButton;

    @FindBy(xpath = "//button[@title='Выбрать банк из списка']")
    private WebElement selectBankButton;


    public MessagePage setBankPMessageCheckBox(boolean value) {
        if(value) bankPMessageCheckBox.click();
        return this;
    }

    public MessagePage setPayerPMessageCheckBox(boolean value) {
        if(value) payerPMessageCheckBox.click();
        return this;
    }

    public MessagePage setKindComboBox(String value) {
        (new Select(kindComboBox)).selectByValue(value);
        return this;
    }

    public MessagePage setIdentifierField(String text) {
        identifierField.sendKeys(text);
        return this;
    }

    public MessagePage setMsgDateField(String date) {
        msgDateField.sendKeys(date);
        return this;
    }

    public MessagePage setInsurerNameField(String text) {
        insurerNameField.sendKeys(text);
        return this;
    }

    public MessagePage setStateRegCertSerField(String text) {
        stateRegCertSerField.sendKeys(text);
        return this;
    }

    public MessagePage setStateRegCertNumField(String text) {
        stateRegCertNumField.sendKeys(text);
        return this;
    }

    public MessagePage setTaxRegCertSerField(String text) {
        taxRegCertSerField.sendKeys(text);
        return this;
    }

    public MessagePage setTaxRegCertNumField(String text) {
        taxRegCertNumField.sendKeys(text);
        return this;
    }


    public MessagePage setBankShortNameField(String text) {
        bankShortNameField.sendKeys(text);
        return this;
    }

    public MessagePage setTaxAuthBankField(String text) {
        taxAuthBankField.sendKeys(text);
        return this;
    }

    public MessagePage setBankKppField(String text) {
        bankKppField.sendKeys(text);
        return this;
    }

    public MessagePage setBankInnField(String text) {
        bankInnField.sendKeys(text);
        return this;
    }

    public MessagePage setBankRepPositionField(String text) {
        bankRepPositionField.sendKeys(text);
        return this;
    }

    public MessagePage setBankRepFullNameField(String text) {
        bankRepFullNameField.sendKeys(text);
        return this;
    }

    public MessagePage setBankPhoneField(String text) {
        bankPhoneField.sendKeys(text);
        return this;
    }

    public MessagePage setAccNumberField(String text) {
        accNumberField.sendKeys(text);
        return this;
    }

    public MessagePage setContractStatusComboBox(String value) {
        (new Select(contractStatusComboBox)).selectByValue(value);
        return this;
    }

    public MessagePage setContractCreationField(String text) {
        contractCreationField.sendKeys(text);
        return this;
    }

    public MessagePage setContractNumberField(String text) {
        contractNumberField.sendKeys(text);
        return this;
    }

    public MessagePage setAccStatusComboBox(String value) {
        (new Select(accStatusComboBox)).selectByValue(value);
        return this;
    }

    public MessagePage setAccCurrComboBox(String value) {
        (new Select(accCurrComboBox)).selectByValue(value);
        return this;
    }

    public MessagePage setAccTypeField(String text) {
        accTypeField.sendKeys(text);
        return this;
    }

    public MessagePage setAccOpenDateField(String date) {
        accOpenDateField.sendKeys(date);
        return this;
    }

    public void saveMessage()
    {
        driver.switchTo().defaultContent();
        saveButton.click();
    }

    public void addBankAccount()
    {
        driver.switchTo().defaultContent();
        addBankAccountButton.click();
    }

    public void closeMessage()
    {
        driver.switchTo().defaultContent();
        closeButton.click();
    }

    public InsurerFilterPage selectInsurer()
    {
        selectInsurerButton.click();
        return pages.insurerFilterPage;
    }

    public BankFilterPage selectBank()
    {
        selectBankButton.click();
        return pages.bankFilterPage;
    }

    public MessagePage ensurePageLoaded() {
        super.ensurePageLoaded();
        driver.switchTo().defaultContent();
        wait.until(presenceOfElementLocated(By.id("appDialogForm")));
        return this;
    }

    public boolean isVisible()
    {
        return messagePage.isDisplayed();
    }

    public MessagePage moveToInnerFrame()
    {
        moveToFrameByLocator(By.xpath("//iframe[@name='appPopupFrame']"));
        return this;
    }

    public String getClientOgrnField() {
        return clientOgrnField.getAttribute("value");
    }

    public String getClientInnField() {
        return clientInnField.getAttribute("value");
    }

    public String getClientKppField() {
        return clientKppField.getAttribute("value");
    }

    public  MessagePage moveToInsurerTab()
    {
        new Actions(driver)
                .moveToElement(insurerTab).click().perform();
        return this;
    }

    public  MessagePage moveToBankTab()
    {
        new Actions(driver)
                .moveToElement(bankTab).click().perform();
        return this;
    }

    public  MessagePage moveToAccountTab()
    {
        new Actions(driver)
                .moveToElement(accountTab).click().perform();
        return this;
    }
}
